package cn.iocoder.yudao.module.member.controller.admin.images;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.images.vo.*;
import cn.iocoder.yudao.module.member.convert.images.ImagesConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.images.ImagesDO;
import cn.iocoder.yudao.module.member.service.images.ImagesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;



@Tag(name = "管理后台 - 用户图片")
@RestController
@RequestMapping("/user/images")
@Validated
public class ImagesController {

    @Resource
    private ImagesService imagesService;

    @PostMapping("/create")
    @Operation(summary = "创建用户图片")
    @PreAuthorize("@ss.hasPermission('user:images:create')")
    public CommonResult<Long> createImages(@Valid @RequestBody ImagesCreateReqVO createReqVO) {
        return success(imagesService.createImages(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户图片")
    @PreAuthorize("@ss.hasPermission('user:images:update')")
    public CommonResult<Boolean> updateImages(@Valid @RequestBody ImagesUpdateReqVO updateReqVO) {
        imagesService.updateImages(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户图片")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('user:images:delete')")
    public CommonResult<Boolean> deleteImages(@RequestParam("id") Long id) {
        imagesService.deleteImages(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户图片")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('user:images:query')")
    public CommonResult<ImagesRespVO> getImages(@RequestParam("id") Long id) {
        ImagesDO images = imagesService.getImages(id);
        return success(ImagesConvert.INSTANCE.convert(images));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户图片列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('user:images:query')")
    public CommonResult<List<ImagesRespVO>> getImagesList(@RequestParam("ids") Collection<Long> ids) {
        List<ImagesDO> list = imagesService.getImagesList(ids);
        return success(ImagesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户图片分页")
    @PreAuthorize("@ss.hasPermission('user:images:query')")
    public CommonResult<PageResult<ImagesRespVO>> getImagesPage(@Valid ImagesPageReqVO pageVO) {
        PageResult<ImagesDO> pageResult = imagesService.getImagesPage(pageVO);
        return success(ImagesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户图片 Excel")
    @PreAuthorize("@ss.hasPermission('user:images:export')")
    @OperateLog(type = EXPORT)
    public void exportImagesExcel(@Valid ImagesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ImagesDO> list = imagesService.getImagesList(exportReqVO);
        // 导出 Excel
        List<ImagesExcelVO> datas = ImagesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户图片.xls", "数据", ImagesExcelVO.class, datas);
    }

}
